package com.shangcheng.dubbo.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shangcheng.dubbo.mappers.SmsCouponProductCategoryRelationMapper;
import com.shangcheng.model.domain.SmsCouponProductCategoryRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsCouponProductCategoryRelationApiImpl implements SmsCouponProductCategoryRelationApi {
    @Autowired
    private SmsCouponProductCategoryRelationMapper couponProductCategoryRelationMapper;

    @Override
    public void create(Long couponId, List<SmsCouponProductCategoryRelation> productCategoryRelationList) {
        for (SmsCouponProductCategoryRelation smsCouponProductCategoryRelation : productCategoryRelationList) {
            smsCouponProductCategoryRelation.setCouponId(couponId);
            couponProductCategoryRelationMapper.insert(smsCouponProductCategoryRelation);
        }
    }

    @Override
    public List<SmsCouponProductCategoryRelation> findById(Integer couponId) {
        return couponProductCategoryRelationMapper.selectList(new LambdaQueryWrapper<SmsCouponProductCategoryRelation>()
                .eq(SmsCouponProductCategoryRelation::getCouponId, couponId));
    }

    @Override
    public void updateByCouponId(Integer couponId, List<SmsCouponProductCategoryRelation> productCategoryRelationList) {
        this.deleteByCouponId(couponId);
        if (ObjectUtil.isNotEmpty(productCategoryRelationList)) {
            for (SmsCouponProductCategoryRelation couponProductCategoryRelation : productCategoryRelationList) {
                couponProductCategoryRelation.setCouponId(Convert.toLong(couponId));
                couponProductCategoryRelationMapper.insert(couponProductCategoryRelation);
            }
        }
    }

    @Override
    public void deleteByCouponId(Integer couponId) {
        couponProductCategoryRelationMapper.delete(new LambdaQueryWrapper<SmsCouponProductCategoryRelation>().eq(SmsCouponProductCategoryRelation::getCouponId, couponId));
    }
}
